<template>
  <div class="channel-edit">
    <!-- 我的频道 -->
    <van-cell>
      <div slot="title" class="title-text">我的频道</div>
      <van-button
        class="edit-btn"
        type="danger"
        plain
        round
        size="mini"
        @click="isEdit = !isEdit"
        >{{ isEdit ? "完成" : "编辑" }}</van-button
      >
    </van-cell>
    <!-- /我的频道 -->

    <!-- 我的频道单元格 -->
    <van-grid :gutter="10" :border="false" class="my-grid">
      <van-grid-item
        class="grid-item"
        v-for="(channel, index) in myChannels"
        :key="index"
        @click="onMyChannelClick(channel, index)"
      >
        <van-icon
          v-show="isEdit && !fixedChannels.includes(channel.id)"
          slot="icon"
          name="clear"
        ></van-icon>
        <span slot="text" :class="{ active: active === index }" class="text">{{
          channel.name
        }}</span>
      </van-grid-item>
    </van-grid>
    <!-- /我的频道单元格 -->

    <!-- 频道推荐 -->
    <van-cell title="频道推荐" class="title-text" :border="false"></van-cell>
    <!-- /频道推荐 -->

    <!-- 频道推荐单元格 -->
    <van-grid :gutter="10" class="recommend-grid">
      <van-grid-item
        class="grid-item"
        v-for="(channel, index) in recommendChannels"
        :key="index"
        icon="plus"
        :text="channel.name"
        @click="onAddChannel(channel)"
      />
    </van-grid>
    <!-- /频道推荐单元格 -->
  </div>
</template>

<script>
import {
  getAllChannels,
  addUserChannel,
  deleteUserChannel,
} from "@/api/channel.js";
import { mapState } from "vuex";
import { setItem } from "@/utils/storage.js";
export default {
  name: "ChannelEdit",
  components: {},
  props: {
    myChannels: {
      type: Array,
      required: true,
    },
    active: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      // 加载所有频道列表
      allChannels: [],
      // 控制编辑状态的显示
      isEdit: false,
      // 固定频道,不允许删除
      fixedChannels: [0],
    };
  },
  computed: {
    recommendChannels() {
      // 频道列表
      const channels = [];
      this.allChannels.forEach((channel) => {
        const ret = this.myChannels.find((myChannel) => {
          return myChannel.id === channel.id;
        });

        // 如果不存在.放入空数组
        if (!ret) {
          channels.push(channel);
        }
      });
      // 计算属性一定要有返回值return
      return channels;
    },

    // 获取user
    ...mapState(["user"]),
  },
  watch: {},
  created() {
    this.locadAllChannels();
  },
  mounted() {},
  methods: {
    // 加载所有频道
    async locadAllChannels() {
      try {
        const { data } = await getAllChannels();
        this.allChannels = data.data.channels;
      } catch (err) {
        this.$toast("获取频道列表数据失败");
      }
    },

    // 点击加号,添加到我的频道
    async onAddChannel(channel) {
      this.myChannels.push(channel);
      if (this.user) {
        try {
          // 已登录,数据存储到线上
          await addUserChannel({
            id: channel.id,
            seq: this.myChannels.length,
          });
          this.$toast("添加成功");
        } catch (err) {
          this.$toast("保存失败");
        }
      } else {
        // 未登录
        setItem("TOUTIAO_CHANNELS", this.myChannels);
      }
    },
    // 点击切换频道
    onMyChannelClick(channel, index) {
      if (this.isEdit) {
        if (this.fixedChannels.includes(channel.id)) {
          return;
        }
        // 编辑状态删除频道
        this.myChannels.splice(index, 1);
        // 会出现后一个索引高亮,解决:
        if (index <= this.active) {
          // 让激活频道的索引 -1
          this.$emit("update-active", this.active - 1, true);
        }

        this.deleteChannel(channel);
      } else {
        // 非编辑状态,执行切换频道
        this.$emit("update-active", index, false);
      }
    },
    // 删除保存频道
    async deleteUserChannel(channel) {
      try {
        if (this.user) {
          // 已登录，将数据保存到线上
          await deleteUserChannel(channel.id);
        } else {
          // 未登录，将数据存储到本地
          setItem("TOUTIAO_CHANNELS", this.myChannels);
        }
      } catch (err) {
        console.log(err);
        this.$toast("删除频道失败，请稍后重试");
      }
    },
  },
};
</script>

<style scoped lang="less">
.channel-edit {
  padding: 85px 0;

  .title-text {
    font-size: 32px;
    color: #333333;
  }

  .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }

  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      background-color: #f4f5f6;
      white-space: nowrap;
      .van-grid-item__icon-wrapper {
        position: unset; // 去掉本身的定位
      }
      .van-grid-item__text,
      .text {
        font-size: 28px;
        color: #222;
        margin-top: 0;
      }
      .active {
        color: red;
      }
    }
  }

  /deep/ .recommend-grid {
    .grid-item {
      .van-grid-item__content {
        flex-direction: row;
        .van-icon-plus {
          font-size: 28px;
          margin-right: 6px;
        }
        .van-grid-item__text {
          margin-top: 0;
        }
      }
    }
  }

  /deep/ .my-grid {
    .grid-item {
      .van-icon-clear {
        position: absolute;
        right: 0;
        top: -8px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
    }
  }
}
</style>
